package org.example.managere.service.feibiao.impl;

import lombok.RequiredArgsConstructor;
import org.example.managere.dto.feibiao.AmAapOutputDto;
import org.example.managere.entity.feibiao.AmAapOutput;
import org.example.managere.repository.feibiao.AmAapOutputRepository;
import org.example.managere.service.feibiao.AmAapOutputService;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 工况特定输出参数表 Service 实现
 */
@Service
@RequiredArgsConstructor
@Transactional
public class AmAapOutputServiceImpl implements AmAapOutputService {
    private final AmAapOutputRepository repository;
    private final ModelMapper modelMapper;

    @Override
    @Transactional(readOnly = true)
    public Page<AmAapOutputDto> findAll(Pageable pageable) {
        return repository.findAll(pageable)
                .map(this::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<AmAapOutputDto> findById(Integer id) {
        return repository.findById(id)
                .map(this::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public List<AmAapOutputDto> findByMotorId(String motorId) {
        return repository.findByMotorId(motorId).stream()
                .map(this::toDto)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public Page<AmAapOutputDto> findByMotorState(AmAapOutput.MotorState motorState, Pageable pageable) {
        return repository.findByMotorState(motorState, pageable)
                .map(this::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<AmAapOutputDto> search(String keyword, Pageable pageable) {
        return repository.search(keyword, pageable)
                .map(this::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<AmAapOutputDto> findByRemainingUsefulLifeLessThan(Float threshold, Pageable pageable) {
        return repository.findByRemainingUsefulLifeLessThan(threshold, pageable)
                .map(this::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<AmAapOutputDto> findHighRiskMotors(BigDecimal threshold, Pageable pageable) {
        return repository.findHighRiskMotors(threshold, pageable)
                .map(this::toDto);
    }

    @Override
    public AmAapOutputDto save(AmAapOutputDto dto) {
        AmAapOutput entity = toEntity(dto);
        entity = repository.save(entity);
        return toDto(entity);
    }

    @Override
    public AmAapOutputDto update(Integer id, AmAapOutputDto dto) {
        if (!repository.existsById(id)) {
            throw new RuntimeException("记录不存在，ID: " + id);
        }
        dto.setId(id);
        AmAapOutput entity = toEntity(dto);
        entity = repository.save(entity);
        return toDto(entity);
    }

    @Override
    public void deleteById(Integer id) {
        repository.deleteById(id);
    }

    /**
     * Entity 转 DTO
     */
    private AmAapOutputDto toDto(AmAapOutput entity) {
        return modelMapper.map(entity, AmAapOutputDto.class);
    }

    /**
     * DTO 转 Entity
     */
    private AmAapOutput toEntity(AmAapOutputDto dto) {
        return modelMapper.map(dto, AmAapOutput.class);
    }
} 